home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / lisp / rmailout.el < prev    next >
Lisp/Scheme  |  1993-07-23  |  8KB  |  208 lines

  1. ;;; rmailout.el --- "RMAIL" mail reader for Emacs: output message to a file.
  2.  
  3. ;; Copyright (C) 1985, 1987, 1993 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: mail
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  22. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Code:
  25.  
  26. ;; Temporary until Emacs always has this variable.
  27. (defvar rmail-delete-after-output nil
  28.   "*Non-nil means automatically delete a message that is copied to a file.")
  29.  
  30. (defvar rmail-output-file-alist nil
  31.   "*Alist matching regexps to suggested output Rmail files.
  32. This is a list of elements of the form (REGEXP . NAME-EXP).
  33. NAME-EXP may be a string constant giving the file name to use,
  34. or more generally it may be any kind of expression that returns
  35. a file name as a string.")
  36.  
  37. ;;; There are functions elsewhere in Emacs that use this function; check
  38. ;;; them out before you change the calling method.
  39. (defun rmail-output-to-rmail-file (file-name &optional count)
  40.   "Append the current message to an Rmail file named FILE-NAME.
  41. If the file does not exist, ask if it should be created.
  42. If file is being visited, the message is appended to the Emacs
  43. buffer visiting that file.
  44. If the file exists and is not an Rmail file, 
  45. the message is appended in inbox format.
  46.  
  47. A prefix argument N says to output N consecutive messages
  48. starting with the current one.  Deleted messages are skipped and don't count."
  49.   (interactive
  50.    (let ((default-file
  51.        (let (answer tail)
  52.          (setq tail rmail-output-file-alist)
  53.          ;; Suggest a file based on a pattern match.
  54.          (while (and tail (not answer))
  55.            (save-excursion
  56.          (goto-char (point-min))
  57.          (if (re-search-forward (car (car tail)) nil t)
  58.              (setq answer (eval (cdr (car tail)))))
  59.          (setq tail (cdr tail))))
  60.          ;; If not suggestions, use same file as last time.
  61.          (or answer rmail-last-rmail-file))))
  62.      (list (read-file-name
  63.         (concat "Output message to Rmail file: (default "
  64.             (file-name-nondirectory default-file)
  65.             ") ")
  66.         (file-name-directory rmail-last-rmail-file)
  67.         default-file)
  68.        (prefix-numeric-value current-prefix-arg))))
  69.   (or count (setq count 1))
  70.   (setq file-name
  71.     (expand-file-name file-name
  72.               (file-name-directory rmail-last-rmail-file)))
  73.   (if (and (file-readable-p file-name) (not (rmail-file-p file-name)))
  74.       (rmail-output file-name count)
  75.     (setq rmail-last-rmail-file file-name)
  76.     (rmail-maybe-set-message-counters)
  77.     (setq file-name (abbreviate-file-name file-name))
  78.     (or (get-file-buffer file-name)
  79.     (file-exists-p file-name)
  80.     (if (yes-or-no-p
  81.          (concat "\"" file-name "\" does not exist, create it? "))
  82.         (let ((file-buffer (create-file-buffer file-name)))
  83.           (save-excursion
  84.         (set-buffer file-buffer)
  85.         (rmail-insert-rmail-file-header)
  86.         (let ((require-final-newline nil))
  87.           (write-region (point-min) (point-max) file-name t 1)))
  88.           (kill-buffer file-buffer))
  89.       (error "Output file does not exist")))
  90.     (while (> count 0)
  91.       (let (redelete)
  92.     (unwind-protect
  93.         (progn
  94.           (save-restriction
  95.         (widen)
  96.         (if (rmail-message-deleted-p rmail-current-message)
  97.             (progn (setq redelete t)
  98.                (rmail-set-attribute "deleted" nil)))
  99.         ;; Decide whether to append to a file or to an Emacs buffer.
  100.         (save-excursion
  101.           (let ((buf (get-file-buffer file-name))
  102.             (cur (current-buffer))
  103.             (beg (1+ (rmail-msgbeg rmail-current-message)))
  104.             (end (1+ (rmail-msgend rmail-current-message))))
  105.             (if (not buf)
  106.             (append-to-file beg end file-name)
  107.               (if (eq buf (current-buffer))
  108.               (error "Can't output message to same file it's already in"))
  109.               ;; File has been visited, in buffer BUF.
  110.               (set-buffer buf)
  111.               (let ((buffer-read-only nil)
  112.                 (msg (and (boundp 'rmail-current-message)
  113.                       rmail-current-message)))
  114.             ;; If MSG is non-nil, buffer is in RMAIL mode.
  115.             (if msg
  116.                 (progn
  117.                   (rmail-maybe-set-message-counters)
  118.                   (widen)
  119.                   (narrow-to-region (point-max) (point-max))
  120.                   (insert-buffer-substring cur beg end)
  121.                   (goto-char (point-min))
  122.                   (widen)
  123.                   (search-backward "\n\^_")
  124.                   (narrow-to-region (point) (point-max))
  125.                   (rmail-count-new-messages t)
  126.                   (rmail-show-message msg))
  127.         ;; Output file not in rmail mode => just insert at the end.
  128.         (narrow-to-region (point-min) (1+ (buffer-size)))
  129.         (goto-char (point-max))
  130.         (insert-buffer-substring cur beg end)))))))
  131.           (rmail-set-attribute "filed" t))
  132.       (if redelete (rmail-set-attribute "deleted" t))))
  133.       (setq count (1- count))
  134.       (if rmail-delete-after-output
  135.       (rmail-delete-forward)
  136.     (if (> count 0)
  137.         (rmail-next-undeleted-message 1))))))
  138.  
  139. ;; Returns t if file FILE is an Rmail file.
  140. (defun rmail-file-p (file)
  141.   (let ((buf (generate-new-buffer " *rmail-file-p*")))
  142.     (unwind-protect
  143.     (save-excursion
  144.       (set-buffer buf)
  145.       (insert-file-contents file nil 0 100)
  146.       (looking-at "BABYL OPTIONS:"))
  147.       (kill-buffer buf))))
  148.  
  149. ;;; There are functions elsewhere in Emacs that use this function; check
  150. ;;; them out before you change the calling method.
  151. (defun rmail-output (file-name &optional count)
  152.   "Append this message to Unix mail file named FILE-NAME.
  153. A prefix argument N says to output N consecutive messages
  154. starting with the current one.  Deleted messages are skipped and don't count.
  155. When called from lisp code, N may be omitted."
  156.   (interactive
  157.    (list (read-file-name
  158.       (concat "Output message to Unix mail file"
  159.           (if rmail-last-file
  160.               (concat " (default "
  161.                   (file-name-nondirectory rmail-last-file)
  162.                   "): " )
  163.             ": "))            
  164.       (and rmail-last-file (file-name-directory rmail-last-file))
  165.       rmail-last-file)
  166.      (prefix-numeric-value current-prefix-arg)))
  167.   (or count (setq count 1))
  168.   (setq file-name
  169.     (expand-file-name file-name
  170.               (and rmail-last-file
  171.                    (file-name-directory rmail-last-file))))
  172.   (if (and (file-readable-p file-name) (rmail-file-p file-name))
  173.       (rmail-output-to-rmail-file file-name count)
  174.     (setq rmail-last-file file-name)
  175.     (while (> count 0)
  176.       (let ((rmailbuf (current-buffer))
  177.         (tembuf (get-buffer-create " rmail-output"))
  178.         (case-fold-search t))
  179.     (save-excursion
  180.       (set-buffer tembuf)
  181.       (erase-buffer)
  182.       (insert-buffer-substring rmailbuf)
  183.       (insert "\n")
  184.       (goto-char (point-min))
  185.       (insert "From "
  186.           (mail-strip-quoted-names (or (mail-fetch-field "from")
  187.                            (mail-fetch-field "really-from")
  188.                            (mail-fetch-field "sender")
  189.                            "unknown"))
  190.           " " (current-time-string) "\n")
  191.       ;; ``Quote'' "\nFrom " as "\n>From "
  192.       ;;  (note that this isn't really quoting, as there is no requirement
  193.       ;;   that "\n[>]+From " be quoted in the same transparent way.)
  194.       (while (search-forward "\nFrom " nil t)
  195.         (forward-char -5)
  196.         (insert ?>))
  197.       (append-to-file (point-min) (point-max) file-name))
  198.     (kill-buffer tembuf))
  199.       (if (equal major-mode 'rmail-mode)
  200.       (rmail-set-attribute "filed" t))
  201.       (setq count (1- count))
  202.       (if rmail-delete-after-output
  203.       (rmail-delete-forward)
  204.     (if (> count 0)
  205.         (rmail-next-undeleted-message 1))))))
  206.  
  207. ;;; rmailout.el ends here
  208.